package com.luck.easyfoodbatch.mvp.cart;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.mvp.model.Collect;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.mvp.cart
 * email：893855882@qq.com
 * data：16/11/12
 */
public class EditOrderAddressPresenter implements EditOrderAddressModel {

    private final String TAG = getClass().getSimpleName();
    private EditOrderAddressView editOrderAddressView;
    private Context mContext;

    public EditOrderAddressPresenter(EditOrderAddressView editOrderAddressView, Context mContext) {
        this.editOrderAddressView = editOrderAddressView;
        this.mContext = mContext;
    }

    @Override
    public void requestEditOrderAddress(String order_ids, String address_id) {
        HttpParams params = new HttpParams();
        params.put("order_ids", order_ids);
        params.put("address_id", address_id);
        CacheMode mode = CacheMode.DEFAULT;
        OkHttpUtils.post(HttpUtil.EDIT_ORDER_ADDRESS)
                .tag(this)
                .cacheMode(mode)
                .cacheKey(TAG)
                .params(params)
                .execute(new JsonCallback(mContext, Collect.class) {

                    @Override
                    public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                        editOrderAddressView.editOrderAddressSuccess((Collect) data);
                        editOrderAddressView.hideLoading();
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        editOrderAddressView.hideLoading();
                        editOrderAddressView.httpStatusError();
                    }
                });
    }
}
